﻿
import java.util.LinkedList;
import java.util.Random;

public class RandomStrategyWithEyes implements Strategy 
{
	private int width;
	private int height;
	private LinkedList<String> Directions;
	
	private void Init(Thing map[][], int xPacman, int yPacman, int xMonster, int yMonster) {
		if(Directions == null) Directions = new LinkedList<String>();
		if(!Directions.contains("Up")) Directions.add("Up");
		if(!Directions.contains("Right")) Directions.add("Right");
		if(!Directions.contains("Down")) Directions.add("Down");
		if(!Directions.contains("Left")) Directions.add("Left");
		width = map.length;
		height = map[0].length;
	}
	
	public String SelectDirection(Thing map[][], int xMonster, int yMonster, int xPacman, int yPacman)
	{
		Init(map,xPacman,yPacman,xMonster,yMonster);
		String WerePacman = PacmanAndMonster(map, xMonster, yMonster, xPacman, yPacman);
		if (!Directions.contains(WerePacman))
		{
			String dir = Directions.get(new Random().nextInt(4));
			if ((dir == "Up") && ((yMonster == 0) || ((map[xMonster][yMonster-1] != null) && (map[xMonster][yMonster-1].getClass() == Wall.class)))) {
				return SelectDirection(map, xMonster, yMonster, xPacman, yPacman);	
			}	
			if ((dir == "Right") && ((xMonster == width-1) || ((map[xMonster+1][yMonster] != null) && (map[xMonster+1][yMonster].getClass() == Wall.class)))) {
				return SelectDirection(map,  xMonster, yMonster, xPacman, yPacman);
			}
			if ((dir == "Down") && ((yMonster == height-1) || ((map[xMonster][yMonster+1] != null) && (map[xMonster][yMonster+1].getClass() == Wall.class)))) {
				return SelectDirection(map, xMonster, yMonster, xPacman, yPacman);
			}
			if ((dir == "Left") && ((xMonster ==0) || ((map[xMonster-1][yMonster] != null) && (map[xMonster-1][yMonster].getClass() == Wall.class)))) {
				return SelectDirection(map,  xMonster, yMonster, xPacman, yPacman);
			}
			return dir;
		}
		else return WerePacman;
	}
	
	 public String PacmanAndMonster(Thing map[][], int xMonster, int yMonster, int xPacman, int yPacman)
	 {
		 int x = xMonster;
		 int y = yMonster;
		 if(xPacman != xMonster && yPacman != yMonster)
		 {
			 return "NotFound";
		 }
		 while (y > 0 && (map[x][y] == null || map[x][y].getClass() != Wall.class))
		 {
			 
			 y--;
			 if  (x == xPacman && y == yPacman)
			 {
				 return "Up";
			 }
			 
		 }
		 
		 
		 x = xMonster;
		 y = yMonster;
		 while (x < width-1 && (map[x][y] == null || map[x][y].getClass() != Wall.class))
		 {
			 x++;
			 if  (x == xPacman && y == yPacman)
			 {
				 return "Right";
			 }
			 
		 }
		 
		 
		 x = xMonster;
		 y = yMonster;
		 while (y < height-1 && (map[x][y] == null || map[x][y].getClass() != Wall.class))
		 {
			 y++;
			 if  (x == xPacman && y == yPacman)
			 {
				 return "Down";
			 }
		 }
		 
		 
		 x = xMonster;
		 y = yMonster;
		 while (x > 0 && (map[x][y] == null || map[x][y].getClass() != Wall.class))
		 {
			 x--;
			 if  (x == xPacman && y == yPacman)
			 {
				 return "Left";
			 }
		 }
		 return "NotFound";
	 }
}
